package com.evon.yardmanagement.ui.action;

import java.io.ByteArrayOutputStream;
import java.util.Collections;
import java.util.List;

import com.evon.yardmanagement.export.ExportDataManager;
import com.evon.yardmanagement.export.KeyBinExportableData;
import com.evon.yardmanagement.model.KeyBin;
import com.evon.yardmanagement.service.KeyBinManagementService;

/**
 * Action class responsible for exporting the key bin data.
 * 
 * @author vgaurav
 * 
 */
public class ExportKeyBinAction extends BaseExportAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private KeyBinManagementService service = new KeyBinManagementService();
	private List<KeyBin> keyBins = Collections.EMPTY_LIST;

	private int selectedYardId;

	@Override
	public ByteArrayOutputStream exportDataToExcelFile() {

		keyBins = getSelectedYardId() != 0 ? service
				.getKeyBinByYardId(getSelectedYardId())
				: Collections.EMPTY_LIST;
		KeyBinExportableData exportableData = new KeyBinExportableData(keyBins);
		try
		{
		ExportDataManager<KeyBinExportableData> exportManager = new ExportDataManager<KeyBinExportableData>(
				exportableData);
		return exportManager.exportToExcel();
		} catch(RuntimeException ex){
			ex.printStackTrace();
			return null;
		}
	}

	public int getSelectedYardId() {
		return selectedYardId;
	}

	public void setSelectedYardId(int selectedYardId) {
		this.selectedYardId = selectedYardId;
	}

}
